package com.yanqu.road.dao.impl.match;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.match.MatchTimeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/2/18 10:34
 */
public class MatchTimeDataDaoImpl extends TempDataObjectDao<MatchTimeData> {
    public static String addSql = "INSERT INTO t_u_variety_time_data(userId, dispatchTimeId, id, getRewardTime) values(?,?,?,?)";
    @Override
    protected boolean addData(MatchTimeData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDispatchTimeId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        para.put(i, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        if (getDb().executeNoneQuery(addSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_variety_time_data set dispatchTimeId = ?,  getRewardTime = ? " +
            "WHERE userId = ? AND id = ?";
    @Override
    protected boolean updateData(MatchTimeData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getDispatchTimeId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i, new DbParameter(Types.INTEGER, data.getId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected MatchTimeData parseData(ResultSet rs) throws SQLException {
        MatchTimeData data = new MatchTimeData();
        data.setUserId(rs.getLong("userId"));
        data.setId(rs.getInt("id"));
        data.setDispatchTimeId(rs.getInt("dispatchTimeId"));
        data.setGetRewardTime(rs.getLong("getRewardTime"));
        return data;
    }

    public static String selectSql = "SELECT * FROM t_u_variety_time_data WHERE userId = ?";
    public Map<Integer, Map<Integer, MatchTimeData>> getMatchTimeData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, MatchTimeData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                MatchTimeData data = parse(rs);
                if (!map.containsKey(data.getDispatchTimeId())) {
                    map.put(data.getDispatchTimeId(), new ConcurrentHashMap<>());
                }
                map.get(data.getDispatchTimeId()).put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
